package managedbean;

import entity.BranchAddressEntity;
import entity.FnBBranchEntity;
import entity.StakeholderEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import session.stateless.BranchSessionBean;
import session.stateless.SystemMessagesLocal;
import util.enumeration.CuisineType;
import util.enumeration.OperatingDay;
import util.enumeration.OperatingHours;
import util.enumhelper.OperatingHrs;
import util.localization.WebLocalization;

@ManagedBean
@ViewScoped
public class AddBranchManagedBean implements Serializable {

    @EJB
    private BranchSessionBean branchSessionBean;
    //Exception
    @EJB
    private SystemMessagesLocal systemMessages;
    //User Selected Locale
    private String preferredLocale;
    private WebLocalization localization;
    private FnBBranchEntity fnbBranch;
    private StakeholderEntity stakeholder;
    private OperatingDay[] operatingDay = OperatingDay.values();
    private OperatingHours mondayStart;
    private OperatingHours mondayEnd;
    private OperatingHours tuesdayStart;
    private OperatingHours tuesdayEnd;
    private OperatingHours wednesdayStart;
    private OperatingHours wednesdayEnd;
    private OperatingHours thursdayStart;
    private OperatingHours thursdayEnd;
    private OperatingHours fridayStart;
    private OperatingHours fridayEnd;
    private OperatingHours saturdayStart;
    private OperatingHours saturdayEnd;
    private OperatingHours sundayStart;
    private OperatingHours sundayEnd;
    private OperatingHours extendedTimeStart;
    private OperatingHours extendedTimeEnd;
    private List<OperatingHrs> operatingHrsList;
    private List<OperatingHrs> extendedOperatingHoursList;
    private List<CuisineType> cuisineTypeList = null;
    private Date extendedCalendarDate;
    private boolean branchAdded;
    private String fnbType;
    private String operatingHoursRemarks;

    public AddBranchManagedBean() {
        /*LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
    }

    public boolean isBranchAdded() {
        return branchAdded;
    }

    public void setBranchAdded(boolean branchAdded) {
        this.branchAdded = branchAdded;
    }

    public BranchSessionBean getBranchSessionBean() {
        return branchSessionBean;
    }

    public void setBranchSessionBean(BranchSessionBean branchSessionBean) {
        this.branchSessionBean = branchSessionBean;
    }

    public String getFnbType() {
        return fnbType;
    }

    public void setFnbType(String fnbType) {
        this.fnbType = fnbType;
    }

    public WebLocalization getLocalization() {
        return localization;
    }

    public void setLocalization(WebLocalization localization) {
        this.localization = localization;
    }

    public OperatingDay[] getOperatingDay() {
        return operatingDay;
    }

    public void setOperatingDay(OperatingDay[] operatingDay) {
        this.operatingDay = operatingDay;
    }

    public String getPreferredLocale() {
        return preferredLocale;
    }

    public void setPreferredLocale(String preferredLocale) {
        this.preferredLocale = preferredLocale;
    }

    public StakeholderEntity getStakeholder() {
        return stakeholder;
    }

    public void setStakeholder(StakeholderEntity stakeholder) {
        this.stakeholder = stakeholder;
    }

    public SystemMessagesLocal getSystemMessages() {
        return systemMessages;
    }

    public void setSystemMessages(SystemMessagesLocal systemMessages) {
        this.systemMessages = systemMessages;
    }

    /***************************************************/
    /*************** GET SET METHODS *******************/
    /***************************************************/
    public FnBBranchEntity getFnbBranch() {
        return fnbBranch;
    }

    public void setFnbBranch(FnBBranchEntity fnbBranch) {
        this.fnbBranch = fnbBranch;
    }

    public OperatingHours getMondayStart() {
        return mondayStart;
    }

    public void setMondayStart(OperatingHours mondayStart) {
        this.mondayStart = mondayStart;
    }

    public OperatingHours getMondayEnd() {
        return mondayEnd;
    }

    public void setMondayEnd(OperatingHours mondayEnd) {
        this.mondayEnd = mondayEnd;
    }

    public OperatingHours getTuesdayStart() {
        return tuesdayStart;
    }

    public void setTuesdayStart(OperatingHours tuesdayStart) {
        this.tuesdayStart = tuesdayStart;
    }

    public OperatingHours getTuesdayEnd() {
        return tuesdayEnd;
    }

    public void setTuesdayEnd(OperatingHours tuesdayEnd) {
        this.tuesdayEnd = tuesdayEnd;
    }

    public OperatingHours getWednesdayStart() {
        return wednesdayStart;
    }

    public void setWednesdayStart(OperatingHours wednesdayStart) {
        this.wednesdayStart = wednesdayStart;
    }

    public OperatingHours getWednesdayEnd() {
        return wednesdayEnd;
    }

    public void setWednesdayEnd(OperatingHours wednesdayEnd) {
        this.wednesdayEnd = wednesdayEnd;
    }

    public OperatingHours getThursdayStart() {
        return thursdayStart;
    }

    public void setThursdayStart(OperatingHours thursdayStart) {
        this.thursdayStart = thursdayStart;
    }

    public OperatingHours getThursdayEnd() {
        return thursdayEnd;
    }

    public void setThursdayEnd(OperatingHours thursdayEnd) {
        this.thursdayEnd = thursdayEnd;
    }

    public OperatingHours getFridayStart() {
        return fridayStart;
    }

    public void setFridayStart(OperatingHours fridayStart) {
        this.fridayStart = fridayStart;
    }

    public OperatingHours getFridayEnd() {
        return fridayEnd;
    }

    public void setFridayEnd(OperatingHours fridayEnd) {
        this.fridayEnd = fridayEnd;
    }

    public OperatingHours getSaturdayStart() {
        return saturdayStart;
    }

    public void setSaturdayStart(OperatingHours saturdayStart) {
        this.saturdayStart = saturdayStart;
    }

    public OperatingHours getSaturdayEnd() {
        return saturdayEnd;
    }

    public void setSaturdayEnd(OperatingHours saturdayEnd) {
        this.saturdayEnd = saturdayEnd;
    }

    public OperatingHours getSundayStart() {
        return sundayStart;
    }

    public void setSundayStart(OperatingHours sundayStart) {
        this.sundayStart = sundayStart;
    }

    public OperatingHours getSundayEnd() {
        return sundayEnd;
    }

    public void setSundayEnd(OperatingHours sundayEnd) {
        this.sundayEnd = sundayEnd;
    }

    public OperatingHours getExtendedTimeStart() {
        return extendedTimeStart;
    }

    public void setExtendedTimeStart(OperatingHours extendedTimeStart) {
        this.extendedTimeStart = extendedTimeStart;
    }

    public OperatingHours getExtendedTimeEnd() {
        return extendedTimeEnd;
    }

    public void setExtendedTimeEnd(OperatingHours extendedTimeEnd) {
        this.extendedTimeEnd = extendedTimeEnd;
    }

    public List<OperatingHrs> getOperatingHrsList() {
        return operatingHrsList;
    }

    public void setOperatingHrsList(List<OperatingHrs> operatingHrsList) {
        this.operatingHrsList = operatingHrsList;
    }

    public List<OperatingHrs> getExtendedOperatingHoursList() {
        return extendedOperatingHoursList;
    }

    public void setExtendedOperatingHoursList(List<OperatingHrs> extendedOperatingHoursList) {
        this.extendedOperatingHoursList = extendedOperatingHoursList;
    }

    public List<CuisineType> getCuisineTypeList() {
        return cuisineTypeList;
    }

    public void setCuisineTypeList(List<CuisineType> cuisineTypeList) {
        this.cuisineTypeList = cuisineTypeList;
    }

    public Date getExtendedCalendarDate() {
        return extendedCalendarDate;
    }

    public void setExtendedCalendarDate(Date extendedCalendarDate) {
        this.extendedCalendarDate = extendedCalendarDate;
    }

    public String getOperatingHoursRemarks() {
        return operatingHoursRemarks;
    }

    public void setOperatingHoursRemarks(String operatingHoursRemarks) {
        this.operatingHoursRemarks = operatingHoursRemarks;
    }

    /************************************************************************************\
    /*********************************** METHODS ****************************************\
    /************************************************************************************/
    public void initView(PhaseEvent event) {

        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        /*
        if (stakeholder == null) {
        stakeholder = (StakeholderEntity) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderEntity");
        }*/
        //Clear previous set messages(delete)
        FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();

        if (getFnbBranch() == null) {
            fnbBranch = new FnBBranchEntity();
            fnbBranch.setBranchAddress(new BranchAddressEntity());

            this.extendedOperatingHoursList = new ArrayList<OperatingHrs>();
            this.cuisineTypeList = new ArrayList<CuisineType>();
            this.operatingHrsList = new ArrayList<OperatingHrs>();
        }
    }

    //Add a specific date list for extended operating hours
    public void addSpecificDateList() {
        OperatingHrs specificOperatingHrs = new OperatingHrs();
        boolean duplicate = false;

        //Check calendar date is empty or not
        if (extendedCalendarDate != null) {
            Date todayDate = new Date();
            //entered calendar date has to be after today's date
            if (extendedCalendarDate.before(todayDate) && !(extendedCalendarDate.equals(todayDate))) {
                String statusMessage = systemMessages.getSystemMessage("EXTENDED_DATE_NOT_BEFORE_TODAY'S_DATE", preferredLocale);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, statusMessage));
            } else {
                //check whether the time entered is equal except for off
                if (extendedTimeStart.getTime().equals(extendedTimeEnd.getTime())
                        && !(extendedTimeStart.getLabel().equals("OFF") && extendedTimeEnd.getLabel().equals("OFF"))) {
                    String statusMessage = systemMessages.getSystemMessage("EXTENDED_START_TIME_END_TIME_MUST_BE_DIFFERENT", preferredLocale);
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, statusMessage));
                } /*else if (extendedTimeStart.getTime().after(extendedTimeEnd.getTime())
                && (!(extendedTimeStart.getLabel().equals("OFF") && !(extendedTimeEnd.getLabel().equals("OFF"))))) {
                //String statusMessage = systemMessages.getSystemMessage("CREATION_SUCCESS", selectedLocale);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Extended Operating Hours: Select an appropriate time range", "Extended Operating Hours: Select an appropriate time range"));
                } */ //check if 1 time is entered off, the other must also be off
                else if ((extendedTimeStart.getLabel().equals("OFF") && !(extendedTimeEnd.getLabel().equals("OFF")))
                        || (!(extendedTimeStart.getLabel().equals("OFF")) && extendedTimeEnd.getLabel().equals("OFF"))) {
                    String statusMessage = systemMessages.getSystemMessage("EXTENDED_START_TIME_END_TIME_MUST_BE_OFF", preferredLocale);
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, statusMessage));
                } // if all goes well....
                else {
                    //Select the day string value from enum(Compare calendar day(int value) to enum day(int value))
                    for (int i = 0; i < operatingDay.length; i++) {
                        if (extendedCalendarDate.getDay() == operatingDay[i].getDay()) {
                            String day = operatingDay[i].getLabel();
                            specificOperatingHrs = new OperatingHrs(operatingDay[i], day, extendedTimeStart, extendedTimeStart.getTime(), extendedTimeEnd, extendedTimeEnd.getTime(), extendedCalendarDate);
                        }
                    }
                    //If there is no record exist in the list, add to arraylist
                    if (extendedOperatingHoursList.isEmpty()) {
                        extendedOperatingHoursList.add(specificOperatingHrs);
                        String statusMessage = systemMessages.getSystemMessage("ADDED_TO_LIST", preferredLocale);
                        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, statusMessage));
                    } else {
                        //traverse array list to search for duplicate record
                        //if exists, set msg and break from loop
                        for (int i = 0; i < extendedOperatingHoursList.size(); i++) {
                            //Check both dates is equal
                            if (extendedOperatingHoursList.get(i).getSpecificDate().equals(specificOperatingHrs.getSpecificDate())
                                    && ( //check new opening time has to be after closing time
                                    (extendedOperatingHoursList.get(i).getClosingHours().after(specificOperatingHrs.getOpeningHours()))
                                    || //check both opening n closing is equal
                                    (extendedOperatingHoursList.get(i).getOpeningHours().equals(specificOperatingHrs.getOpeningHours())
                                    && extendedOperatingHoursList.get(i).getClosingHours().equals(specificOperatingHrs.getClosingHours())))) {
                                //if all true, return duplicate msg
                                String statusMessage = systemMessages.getSystemMessage("DUPLICATE_DATE", preferredLocale);
                                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, statusMessage));
                                duplicate = true;
                                break;
                            }
                        }
                        //if there's no duplicate records in arraylist, add new record into list
                        if (duplicate == false) {
                            extendedOperatingHoursList.add(specificOperatingHrs);
                            String statusMessage = systemMessages.getSystemMessage("ADDED_TO_LIST", preferredLocale);
                            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, statusMessage));
                        }
                    }
                }
            }
        } else {
            String statusMessage = systemMessages.getSystemMessage("EXTENDED_IS_NOT_SELECTED", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, statusMessage));
        }
    }

    /*Remove a specific date list from the datatable*/
    public void removeSpecificDateList(OperatingHrs extendedOperatingHrs) {
        extendedOperatingHoursList.remove(extendedOperatingHrs);
        String statusMessage = systemMessages.getSystemMessage("REMOVED_FROM_LIST", preferredLocale);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, statusMessage));
    }

    public void saveNewBranch() {
        this.operatingHrsList = new ArrayList<OperatingHrs>();

        OperatingHrs monOpHrs = new OperatingHrs(operatingDay[0], operatingDay[0].getLabel(), getMondayStart(), getMondayStart().getTime(), getMondayEnd(), getMondayEnd().getTime());
        OperatingHrs tuesOpHrs = new OperatingHrs(operatingDay[1], operatingDay[1].getLabel(), getTuesdayStart(), getTuesdayStart().getTime(), getTuesdayEnd(), getTuesdayEnd().getTime());
        OperatingHrs wedOpHrs = new OperatingHrs(operatingDay[2], operatingDay[2].getLabel(), getWednesdayStart(), getWednesdayStart().getTime(), getWednesdayEnd(), getWednesdayEnd().getTime());
        OperatingHrs thursOpHrs = new OperatingHrs(operatingDay[3], operatingDay[3].getLabel(), getThursdayStart(), getThursdayStart().getTime(), getThursdayEnd(), getThursdayEnd().getTime());
        OperatingHrs friOpHrs = new OperatingHrs(operatingDay[4], operatingDay[4].getLabel(), getFridayStart(), getFridayStart().getTime(), getFridayEnd(), getFridayEnd().getTime());
        OperatingHrs satOpHrs = new OperatingHrs(operatingDay[5], operatingDay[5].getLabel(), getSaturdayStart(), getSaturdayStart().getTime(), getSaturdayEnd(), getSaturdayEnd().getTime());
        OperatingHrs sunOpHrs = new OperatingHrs(operatingDay[6], operatingDay[6].getLabel(), getSundayStart(), getSundayStart().getTime(), getSundayEnd(), getSundayEnd().getTime());
        getOperatingHrsList().add(monOpHrs);
        getOperatingHrsList().add(tuesOpHrs);
        getOperatingHrsList().add(wedOpHrs);
        getOperatingHrsList().add(thursOpHrs);
        getOperatingHrsList().add(friOpHrs);
        getOperatingHrsList().add(satOpHrs);
        getOperatingHrsList().add(sunOpHrs);

        branchAdded = branchSessionBean.addBranch(getFnbBranch().getBranchName(), getFnbBranch().getBranchAddress().getPostalCode(), getFnbBranch().getBranchAddress().getUnitNumber(), getFnbBranch().getBranchAddress().getHouseNumber(),
                getFnbBranch().getBranchAddress().getStreetName(), getFnbBranch().getBranchAddress().getBuildingName(), getFnbBranch().getContactNumber(), getFnbBranch().getContactPerson(),
                getFnbBranch().getEmailAddress(), getFnbBranch().getFnBTypeEnum(), getFnbBranch().getIsHalalEnum(), getFnbBranch().getPriceRangeEnum(),
                preferredLocale, operatingHoursRemarks, getCuisineTypeList(), getOperatingHrsList(), getExtendedOperatingHoursList() //StakeholderEntity stakeholder
                );

        if (branchAdded == true) {
            String statusMessage = systemMessages.getSystemMessage("BRANCH_CREATION_SUCCESS", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
            reset();
        } else {
            String statusMessage = systemMessages.getSystemMessage("BRANCH_CREATION_UNSUCCESSFUL", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
        }
    }

    /*Reset the values of all the fields after adding into the database*/
    public void reset() {
        fnbBranch = new FnBBranchEntity();
        fnbBranch.setBranchAddress(new BranchAddressEntity());
        setOperatingHoursRemarks("");
        cuisineTypeList.clear();
        // TODO:
        //getFnbBranch().setFnBTypeEnum(FnbType.SELECT);
        //getFnbBranch().setIsHalal();
        //getFnbBranch().setPriceRange();
        setMondayStart(OperatingHours.EightAM);
        setMondayEnd(OperatingHours.EightAM);
        setTuesdayStart(OperatingHours.EightAM);
        setTuesdayEnd(OperatingHours.EightAM);
        setWednesdayStart(OperatingHours.EightAM);
        setWednesdayEnd(OperatingHours.EightAM);
        setThursdayStart(OperatingHours.EightAM);
        setThursdayEnd(OperatingHours.EightAM);
        setFridayStart(OperatingHours.EightAM);
        setFridayEnd(OperatingHours.EightAM);
        setSaturdayStart(OperatingHours.EightAM);
        setSaturdayEnd(OperatingHours.EightAM);
        setSundayStart(OperatingHours.EightAM);
        setSundayEnd(OperatingHours.EightAM);
        setExtendedCalendarDate(null);
        setExtendedTimeStart(OperatingHours.EightAM);
        setExtendedTimeEnd(OperatingHours.EightAM);
        extendedOperatingHoursList.clear();
    }

    public String getButton() {
        List<FnBBranchEntity> branchList = new ArrayList();
        branchList = branchSessionBean.viewAllBranch();
        if (!branchList.isEmpty()) {
            return "Add More Branches";
        } else {
            return "Add A New Branch";
        }
    }

    /************************************************************************************|
    |*********************************** VALIDATION METHODS *****************************|
    |************************************************************************************/
    public void validateOperatingHours(FacesContext context, UIComponent component, Object value) {
        //Get the specific component ID
        String ID = component.getId();
        //Get the end time of the operating hours
        OperatingHours endTime = (OperatingHours) value;
        //Check the component ID
        if (ID.contains("Monday")) {
            String msg = validateOpHrs(component, endTime);
            //if message is not empty, display.
            if (!msg.isEmpty()) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
                context.addMessage(component.getClientId(context), message);
            }
        } else if (ID.contains("Tuesday")) {
            String msg = validateOpHrs(component, endTime);
            //if message is not empty, display.
            if (!msg.isEmpty()) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
                context.addMessage(component.getClientId(context), message);
            }
        } else if (ID.contains("Wednesday")) {
            String msg = validateOpHrs(component, endTime);
            //if message is not empty, display.
            if (!msg.isEmpty()) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
                context.addMessage(component.getClientId(context), message);
            }
        } else if (ID.contains("Thursday")) {
            String msg = validateOpHrs(component, endTime);
            //if message is not empty, display.
            if (!msg.isEmpty()) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
                context.addMessage(component.getClientId(context), message);
            }
        } else if (ID.contains("Friday")) {
            String msg = validateOpHrs(component, endTime);
            //if message is not empty, display.
            if (!msg.isEmpty()) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
                context.addMessage(component.getClientId(context), message);
            }
        } else if (ID.contains("Saturday")) {
            String msg = validateOpHrs(component, endTime);
            //if message is not empty, display.
            if (!msg.isEmpty()) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
                context.addMessage(component.getClientId(context), message);
            }
        } else if (ID.contains("Sunday")) {
            String msg = validateOpHrs(component, endTime);
            //if message is not empty, display.
            if (!msg.isEmpty()) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
                context.addMessage(component.getClientId(context), message);
            }
        }
    }

    public String validateOpHrs(UIComponent component, OperatingHours endTime) {
        UIInput day = (UIInput) component.getAttributes().get("day");
        OperatingHours startTime = (OperatingHours) day.getValue();

        //If either start time of end time is off
        if (startTime.getLabel().equals("OFF") || endTime.getLabel().equals("OFF")) {
            //if confirm both is off
            if (startTime.getLabel().equals("OFF") && endTime.getLabel().equals("OFF")) {
                return "";
            } else {
                //set the component that value is deemed incorrect to false to disallow submitting of page
                ((UIInput) component).setValid(false);
                //return error message
                String statusMessage = systemMessages.getSystemMessage("OPERATING_HOURS_START_TIME_END_TIME_MUST_BE_OFF", preferredLocale);
                return statusMessage;
            }
        } else if (startTime.getTime().equals(endTime.getTime())) {
            //set the component that value is deemed incorrect to false to disallow submitting of page
            ((UIInput) component).setValid(false);
            //return error message
            String statusMessage = systemMessages.getSystemMessage("OPERATING_HOURS_START_TIME_END_TIME_MUST_BE_DIFFERENT", preferredLocale);
            return statusMessage;
        }
        return "";
    }

    public void validateEmail(FacesContext context, UIComponent toValidate, Object value) {
        String email = (String) value;
        Pattern p = Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");

        //Match the given string with the pattern
        Matcher m = p.matcher(email);

        //Check whether match is found
        boolean matchFound = m.matches();

        //if mail does not match the pattern
        if (!matchFound) {
            ((UIInput) toValidate).setValid(false);
            String statusMessage = systemMessages.getSystemMessage("INVALID_EMAIL_ADDRESS", preferredLocale);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, statusMessage);
            context.addMessage(toValidate.getClientId(context), message);
        }
    }

    public void validateBranchName(FacesContext context, UIComponent toValidate, Object value) {
        String name = (String) value;

        List<FnBBranchEntity> branchExists = branchSessionBean.getFnBBranchList();
        for (int i = 0; i < branchExists.size(); i++) {
            if (branchExists.get(i).getBranchName().equalsIgnoreCase(name)) {
                ((UIInput) toValidate).setValid(false);
                String statusMessage = systemMessages.getSystemMessage("BRANCH_NAME_EXISTS", preferredLocale);
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, statusMessage);
                context.addMessage(toValidate.getClientId(context), message);
            }
        }
    }
}
